package com.meilingcloud.biobank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilingcloud.biobank.domain.SampleGroup;
import com.meilingcloud.biobank.mapper.SampleGroupMapper;
import com.meilingcloud.biobank.service.ISampleGroupService;
import com.meilingcloud.common.constant.Constants;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 样本组 Service业务层处理
 *
 * @author chenl
 * @date 2021-03-02
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class SampleGroupServiceImpl extends ServiceImpl<SampleGroupMapper, SampleGroup> implements ISampleGroupService {

    @Override
    public List<SampleGroup> queryList(SampleGroup sampleGroup) {
        LambdaQueryWrapper<SampleGroup> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sampleGroup.getSampGroupName())) {
            lqw.like(SampleGroup::getSampGroupName, sampleGroup.getSampGroupName());
        }
        if (StringUtils.isNotBlank(sampleGroup.getSampGroupDesc())) {
            lqw.eq(SampleGroup::getSampGroupDesc, sampleGroup.getSampGroupDesc());
        }
        List<SampleGroup> sampleGroups = this.list(lqw);
        return sampleGroups;
    }

    @Cacheable(value = Constants.SAMPLE_GROUP_KEY, key = "#sampGroupId", unless = "#result == null ", cacheManager = "cacheManager1d")
    @Override
    public SampleGroup queryById(Long sampGroupId) {
        return this.getById(sampGroupId);
    }

    @Cacheable(value = Constants.SAMPLE_GROUP_KEY, key = "#code", unless = "#result == null ", cacheManager = "cacheManager1d")
    @Override
    public SampleGroup queryByCode(String code) {
        return this.getOne(new LambdaQueryWrapper<SampleGroup>().eq(SampleGroup::getSampGroupCode, code).last("limit 1"));
    }

    @CacheEvict(value = Constants.SAMPLE_GROUP_KEY, allEntries = true)
    @Override
    public boolean updateSampGroupById(SampleGroup sampleGroup) {
        return this.updateById(sampleGroup);
    }

    @CacheEvict(value = Constants.SAMPLE_GROUP_KEY, allEntries = true)
    @Override
    public boolean removeSampGroupByIds(List<Long> sampGroupIds) {
        return this.removeByIds(sampGroupIds);
    }


}
